﻿using System;

namespace SelectSort.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 9, 8, 7, 8, 7, 6, 7, 6, 5, 6, 5, 4 };
            //int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("Hello World!");
            Console.WriteLine("before:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine("");
            DateTime StartTime = DateTime.Now;
            SelectSort(arr, arr.Length);
            DateTime EndTime = DateTime.Now;
            TimeSpan ts = EndTime - StartTime;
            Console.WriteLine("after:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine("");
            Console.WriteLine("Time Spend:" + ts);
            Console.ReadLine();
        }
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="length"></param>
        static void SelectSort(int[] arr, int length)
        {
            int min = 0;
            for (int i = 0; i < length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < length; j++)
                {
                    if (arr[j] < arr[min])
                        min = j;
                }
                if (min != i)
                {
                    SortHelper.Swap(arr, min, i);
                }
            }
        }


    }
}